package com.rstrulak.managedBeans.controllers;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ProjectAttachmentsDao;
import com.rstrulak.dao.ProjectDao;
import com.rstrulak.model.Project;
import com.rstrulak.model.ProjectAttachments;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;
import com.rstrulak.utils.NoteFile;

@Component("editProjectBean")
@Scope("request")
public class EditProjectBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3500448389410321012L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private ProjectAttachmentsDao projectAttachmentsDao;

	private Project project;

	private String projectName;

	private String projectDescription;

	private List<ProjectAttachments> projectAttachmentsList;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();

		Long projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);

		if (projectId == null) {
			JSFUtils.setFlashFromSession();
			projectId = (Long) JSFUtils.flashScope().get(Constants.PROJECT_ID);
			JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		}

		getProjectFromDb(projectId);

		if (project != null) {
			projectAttachmentsList = projectAttachmentsDao.getProjectAttachemntsListByProject(project);
		}
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public List<ProjectAttachments> getProjectAttachmentsList() {
		return projectAttachmentsList;
	}

	public void setProjectAttachmentsList(List<ProjectAttachments> projectAttachmentsList) {
		this.projectAttachmentsList = projectAttachmentsList;
	}

	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}

	private void getProjectFromDb(Long projectId) {
		this.project = projectDao.getById(projectId);
		projectName = project.getName();
		projectDescription = project.getDescription();
		JSFUtils.setSessionParams();
	}

	public void updateProject(FileUploadBean fileUploadBean) throws ServletException, IOException {
		project.setDescription(projectDescription);
		project.setName(projectName);
		projectDao.saveOrUpdate(project);

		FileUploadBean uploadBean = fileUploadBean;
		ArrayList<NoteFile> filesArray = uploadBean.getFiles();
		List<ProjectAttachments> projectAttachmentsList = new ArrayList<ProjectAttachments>();

		for (int i = 0; i < filesArray.size(); i++) {
			ProjectAttachments projectAttachments = new ProjectAttachments();
			String oldName = filesArray.get(i).getName();
			StringBuilder finalName = new StringBuilder();
			finalName.append("projectId_");
			finalName.append(project.getId());
			finalName.append("_fileName_");
			finalName.append(oldName);

			projectAttachments.setProject(project);
			projectAttachments.setFilePath(Constants.PROJECT_ATTACHMENTS_PATH + finalName);
			projectAttachments.setFileName(oldName);
			projectAttachmentsList.add(projectAttachments);

			filesArray.get(i).setName(finalName.toString());
		}

		fileUploadBean.saveProjectFiles();
		fileUploadBean.clearUploadData();
		if (projectAttachmentsList.size() > 0) {
			this.saveProjectAttachments(projectAttachmentsList);
		}

		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

	public void cancelEditProject() throws ServletException, IOException {
		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

	public void deleteAttachment(ProjectAttachments projectAttachments) {
		projectAttachmentsDao.delete(projectAttachments);
		File file = new File(projectAttachments.getFilePath());
		file.delete();
		projectAttachmentsList = projectAttachmentsDao.getProjectAttachemntsListByProject(project);
	}

	public void saveProjectAttachments(List<ProjectAttachments> projectAttachmentsList) {
		for (ProjectAttachments projectAttachments : projectAttachmentsList) {
			projectAttachmentsDao.save(projectAttachments);
		}
	}

}
